package com.ruoyi.business.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.ruoyi.business.alarm.websocket.AlarmWebSocketHandler;

/**
 * WebSocket配置类
 * 使用Spring WebSocket API，兼容Undertow和Tomcat
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private static final Logger log = LoggerFactory.getLogger(WebSocketConfig.class);

    @Autowired
    private AlarmWebSocketHandler alarmWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        log.info("注册WebSocket处理器，路径: /websocket/alarm");
        
        // 注册WebSocket处理器
        // 允许所有来源（生产环境应该限制为特定域名）
        registry.addHandler(alarmWebSocketHandler, "/websocket/alarm")
                .setAllowedOrigins("*"); // 允许跨域，生产环境应该设置为具体的前端域名
        
        log.info("WebSocket处理器注册成功，路径: /websocket/alarm");
    }
}

